package deleteface.bank.jqsoft.cn.deleteface.webservice;

import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;


import org.json.JSONException;
import org.json.JSONObject;

import java.security.PublicKey;
import java.util.Calendar;
import java.util.Map;

import deleteface.bank.jqsoft.cn.deleteface.base.BphsConstants;
import deleteface.bank.jqsoft.cn.deleteface.rsa.RsaHelper;
import deleteface.bank.jqsoft.cn.deleteface.setip.Constants;

/**
 *
 */
public class Node {
    //"<"节点转义
    private static String toStart(String name) {
        return "<" + name + ">";
    }

    //">"节点转义
    private static String toEnd(String name) {
        return "</" + name + ">";
    }

    //请求参数拼接
    public static String getRequestParams(String namespace, Map<String, String> map, String gkey) {
        //节点转义
//        if(map==null){
//            map=new HashMap<>();
//        }
//        StringBuffer sbf=new StringBuffer();
//        for(Map.Entry<String,String> entry:map.entrySet()){
//            sbf.append(Node.toStart(entry.getKey()));
//            sbf.append(entry.getValue());
//            sbf.append(Node.toEnd(entry.getKey()));
//        }
        PublicKey publicKey = RsaHelper.decodePublicKeyFromXml( Constants.PUBLICKEY);
//     分段加密 新参数
        JSONObject encryptJsondata = new JSONObject();
        String userAccount="";

            userAccount="admin";

        try {
            encryptJsondata.put("businessId",gkey);
            encryptJsondata.put("serviceTypeId","4026AA324E5F4130BEE560AD888EE720");
            encryptJsondata.put("userAccount", userAccount);
            encryptJsondata.put("hosptalCode","");
            long timestamp= Calendar.getInstance().getTimeInMillis();
            String temp = timestamp + "";
            Log.d("timestamp", temp);
            encryptJsondata.put("timestamp", temp.substring(0, 10));
            encryptJsondata.put("offLineCall","0");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String str = RsaHelper.encryptDataFromStr(encryptJsondata.toString(), publicKey);

        Log.e("Net", "Request:" + namespace + "  json:" + map.toString());
        String str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "  <soap:Body>\n" +
                "    <" + namespace + " xmlns=\"http://tempuri.org/\">\n" +
                "      <strJson>" + JSON.toJSONString(map) + "</strJson>\n" +
                "      <encryptJson>" + str +"</encryptJson>\n" +
                "    </" + namespace + ">\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";


        return str1;
    }

//    /**
//     * 需要传参类
//     */
//    public static String getRequestParams(String namespace, String json) {
//        Log.e("Net", "Request:" + namespace + "  json:" + json);
//        String str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
//                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
//                "  <soap:Body>\n" +
//                "    <" + namespace + " xmlns=\"http://tempuri.org/\">\n" +
//                "      <strJson>" + json + "</strJson>\n" +
//                "    </" + namespace + ">\n" +
//                "  </soap:Body>\n" +
//                "</soap:Envelope>";
//
//
//        return str1;
//    }
    /**
     * 不需要传参类
     */
    public static String getRequestNoJsonParams(String namespace) {
        PublicKey publicKey = RsaHelper.decodePublicKeyFromXml( Constants.PUBLICKEY);
//     分段加密 新参数
        JSONObject encryptJsondata = new JSONObject();

        try {
            encryptJsondata.put("businessId","");
            encryptJsondata.put("serviceTypeId","4026AA324E5F4130BEE560AD888EE720");
            encryptJsondata.put("userAccount","admin");
            encryptJsondata.put("hosptalCode","");
            long timestamp= Calendar.getInstance().getTimeInMillis();
            String temp = timestamp + "";
            Log.d("timestamp", temp);
            encryptJsondata.put("timestamp", temp.substring(0, 10));
            encryptJsondata.put("offLineCall","0");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String str = RsaHelper.encryptDataFromStr(encryptJsondata.toString(), publicKey);
        String str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "  <soap:Body>\n" +
                "    <" + namespace + " xmlns=\"http://tempuri.org/\">\n" +
                "      <encryptJson>" + str +"</encryptJson>\n" +
                "    </" + namespace + ">\n" +

                "  </soap:Body>\n" +
                "</soap:Envelope>";


        return str1;
    }
    //加密用

    public static String getRequestParams(String namespace, String json, String gkey){
        PublicKey publicKey = RsaHelper.decodePublicKeyFromXml( Constants.PUBLICKEY);
        Boolean loginState;
        String offLineCall;
        loginState= BphsConstants.getLoginState();
        if(loginState!=null){
            if (loginState) {
                offLineCall = "0";
            }else{
                offLineCall="1";
            }
        }else{
            offLineCall = "0";
        }

//     分段加密 新参数
        JSONObject encryptJsondata = new JSONObject();
        String userAccount="";

            userAccount="admin";

        try {
            encryptJsondata.put("businessId",gkey);
            encryptJsondata.put("serviceTypeId","4026AA324E5F4130BEE560AD888EE720");
            encryptJsondata.put("userAccount", userAccount);
            encryptJsondata.put("hosptalCode","");
            long timestamp= Calendar.getInstance().getTimeInMillis();
            String temp = timestamp + "";
           Log.d("timestamp", temp);
            encryptJsondata.put("timestamp", temp.substring(0, 10));
            encryptJsondata.put("offLineCall",offLineCall);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String str = RsaHelper.encryptDataFromStr(encryptJsondata.toString(), publicKey);
//         String str = RsaHelper.encryptDataFromStr(encryptJsondata.toString(), publicKey);
//        String str = RsaHelper.encryptDataFromStr(gkey, publicKey);

        String str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "  <soap:Body>\n" +
                "    <" + namespace + " xmlns=\"http://tempuri.org/\">\n" +
                "      <strJson>" + json+ "</strJson>\n" +
                "      <encryptJson>" + str +"</encryptJson>\n" +
                "    </" + namespace + ">\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";



        return str1;
    }
    public static String getRequestParams(String namespace, String json) {
        Log.e("Net", "Request:" + namespace + "  json:" + json);
        String str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "  <soap:Body>\n" +
                "    <" + namespace + " xmlns=\"http://tempuri.org/\">\n" +
                "      <strJson>" + json + "</strJson>\n" +
                "    </" + namespace + ">\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";



        return str1;
    }


}
